package com.example.testsystem;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class NetworkActivity extends Activity {
    private static final String TAG = "TestSystemNetworkActivity";
    private Button btnRegisterNetworkCallback;
    private Context mContext = this;
    private ConnectivityManager mCM;
    private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
            super.onCapabilitiesChanged(network, networkCapabilities);
            Log.i(TAG, "onCapabilitiesChanged, network = " + network + " networkCapabilities = " + networkCapabilities);
            if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                Log.i(TAG, "onCapabilitiesChanged, has NET_CAPABILITY_INTERNET and NET_CAPABILITY_VALIDATED");
            }

            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                Log.i(TAG, "onCapabilitiesChanged, Transport is wifi");
            }

            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                Log.i(TAG, "onCapabilitiesChanged, Transport is ETHERNET");
            }
        }
        @Override
        public void onAvailable(Network network) {
            super.onAvailable(network);
            Log.i(TAG, "onAvailable, network = " + network);
        }
        @Override
        public void onUnavailable() {
            super.onUnavailable();
            Log.i(TAG, "onUnavailable");
        }
        @Override
        public void onLosing(Network network, int maxMsToLive) {
            super.onLosing(network, maxMsToLive);
            Log.i(TAG, "onAvailable, network = " + network);
        }
        @Override
        public void onLost(Network network) {
            super.onLost(network);
            Log.i(TAG, "onLost, network = " + network);
        }

        @Override
        public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
            super.onLinkPropertiesChanged(network, linkProperties);
            Log.i(TAG, "onLinkPropertiesChanged, network = " + network + " linkProperties = " + linkProperties);
        }
        @Override
        public void onBlockedStatusChanged(Network network, boolean blocked) {
            super.onBlockedStatusChanged(network, blocked);
            Log.i(TAG, "onBlockedStatusChanged, network = " + network + " blocked = " + blocked);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);

        mCM = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mCM != null) {
            Log.i(TAG, "registerDefaultNetworkCallback");
            mCM.registerDefaultNetworkCallback(networkCallback);
        }

        btnRegisterNetworkCallback = findViewById(R.id.btn_NetworkCallback);
        btnRegisterNetworkCallback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "btnRegisterNetworkCallback click...");

            }
        });
    }
}